// © 2009 Microsoft Corporation.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Security.Application;
using System.Web.UI;
using System.Configuration;
using Microsoft.InformationSecurity.RiskManagement.ISRMWeb.Resources;
using System.Resources;
using System.Web.UI.WebControls;
using AjaxControlToolkit;


namespace Microsoft.InformationSecurity.RiskManagement.ISRMWeb.Common
{
    public static class Utility
    {
        public static void ShowMessage(MasterPage page, string message) 
        {
            ShowMessage(page, message, "Information", false);
        }

        public static void ShowMessage(MasterPage page, string message, string headerMessage)
        {
            Panel pnl = (Panel)page.FindControl("pnlError");
            if (pnl != null)
            {
                Label lblMessage = (Label)pnl.FindControl("lblMessageTextServerSide");
                if (lblMessage != null)
                    lblMessage.Text = message;

                if (headerMessage != null)
                {
                    Label lblheaderMessage = (Label)pnl.FindControl("lblMessgeHeaderError");
                    if (lblheaderMessage != null)
                        lblheaderMessage.Text = headerMessage;
                }
                
                UpdatePanel upd = page.FindControl("updClientMessage") as UpdatePanel;
                upd.Update();
            }
            ModalPopupExtender modalMessage = (ModalPopupExtender)page.FindControl("mpError");
            if (modalMessage != null)
                modalMessage.Show();
        }

        public static void ShowMessage(MasterPage page, string message, string headerMessage, bool isError)
        {
            Panel pnl = (Panel)page.FindControl("pnlError");
            if (pnl != null)
            {
                Literal lblMessage = (Literal)pnl.FindControl("lblMessageTextServerSide");
                if(lblMessage != null)
                    lblMessage.Text = message;

                if (headerMessage != null)
                {
                    Label lblheaderMessage = (Label)pnl.FindControl("lblMessgeHeaderError");
                    if (lblheaderMessage != null)
                        lblheaderMessage.Text = headerMessage;
                }

                Image information = (Image)pnl.FindControl("imgInformation");
                if (information != null)
                {
                    if(isError)                    
                        information.Visible = false;
                    else
                        information.Visible = true;
                }

                Image errorImg = (Image)pnl.FindControl("imgError");
                if (errorImg != null)
                {
                    if (isError)
                        errorImg.Visible = true;
                    else
                        errorImg.Visible = false;
                }

                UpdatePanel upd = page.FindControl("updClientMessage") as UpdatePanel;
                upd.Update();
            }
            ModalPopupExtender modalMessage = (ModalPopupExtender)page.FindControl("mpError");
            if(modalMessage != null)
                modalMessage.Show();
        }

        /// <summary>
        /// Returns logged in username in the domain\user format.
        /// </summary>
        /// <returns>Username</returns>
        public static string GetUsername() 
        {
            return GetUsername(true);
        }

        /// <summary>
        /// Returns logged in username
        /// </summary>
        /// <param name="includeDomain">Includes domain portion of the username</param>
        /// <returns>Username</returns>
        public static string GetUsername(bool includeDomain)
        {
            if (includeDomain == true)
                return HttpContext.Current.User.Identity.Name;
            else
                return HttpContext.Current.User.Identity.Name.Substring(HttpContext.Current.User.Identity.Name.IndexOf("\\") + 1);
        }

        public static string GetConfigurationValue(string configKey) 
        {
            return ConfigurationManager.AppSettings[configKey];
        }

        /// <summary>
        /// Returns resource value for the specified key.
        /// </summary>
        /// <param name="resourceAssemblyName">Fully qualified name of the resouce assembly. Ex. Microsoft.InformationSecurity.RiskManagement.ISRMWeb.Resources.TasksStrings</param>
        /// <param name="resourceKey">Key name of the resource string.</param>
        /// <returns></returns>
        public static string GetResourceString(string resourceAssemblyName, string resourceKey) 
        {
            System.Resources.ResourceManager resMan = new System.Resources.ResourceManager(resourceAssemblyName, typeof(Utility).Assembly);
            return Microsoft.Security.Application.AntiXss.HtmlEncode(resMan.GetString(resourceKey));
        }

        /// <summary>
        /// Returns resource value for the specified key. Includes overloaded parameter to identify the correct encoding context.
        /// </summary>
        /// <param name="resourceAssemblyName">Fully qualified name of the resouce assembly. Ex. Microsoft.InformationSecurity.RiskManagement.ISRMWeb.Resources.TasksStrings</param>
        /// <param name="resourceKey">Key name of the resource string.</param>
        /// <param name="encodingContext">Integer identifying the type of encoding context. Acceptable values 1 - HtmlEncode, 2 - HtmlAttributeEncode, 3 - UrlEncode, 4 - JavaScriptEncode (does not include quotes).</param>
        /// <returns></returns>
        public static string GetResourceString(string resourceAssemblyName, string resourceKey,int encodingContext)
        {
            System.Resources.ResourceManager resMan = new System.Resources.ResourceManager(resourceAssemblyName, typeof(Utility).Assembly);
            switch (encodingContext)
            {
                case 1:
                    return Microsoft.Security.Application.AntiXss.HtmlEncode(resMan.GetString(resourceKey));
                case 2:
                    return Microsoft.Security.Application.AntiXss.HtmlAttributeEncode(resMan.GetString(resourceKey));
                case 3:
                    return Microsoft.Security.Application.AntiXss.UrlEncode(resMan.GetString(resourceKey));
                case 4:
                    return Microsoft.Security.Application.AntiXss.JavaScriptEncode(resMan.GetString(resourceKey),false);
                default:
                    return Microsoft.Security.Application.AntiXss.HtmlEncode(resMan.GetString(resourceKey));
            }
        }

        

        /// <summary>
        /// Trims a string to certain length and adds elipssis at the end.
        /// </summary>
        /// <param name="input">Input string</param>
        /// <param name="length">Length to trim the string</param>
        /// <returns>Trimmed string</returns>
        public static string TrimString(string input, int length) 
        {
            return TrimString(input, length,true);
        }

        /// <summary>
        /// Trims a string to certain length and adds elipssis at the end.
        /// </summary>
        /// <param name="input">Input string</param>
        /// <param name="length">Length to trim the string</param>
        /// <param name="addElipsis">Adds elipsis at the end of the string</param>
        /// <returns>Trimmed string</returns>
        public static string TrimString(string input, int length,bool addElipsis)
        {
            string trimmedString = "";
            if (input.Length > length)
            {
                if (addElipsis)
                    trimmedString = input.Substring(0, length) + "...";
                else
                    trimmedString = input.Substring(0, length);
            }
            else
                trimmedString = input;
            return trimmedString;
        }

        /// <summary>
        /// Returns logged in user's fullname
        /// </summary>
        /// <returns>User's Full Name</returns>
        public static string GetUserFullName() 
        {
            return ((CISF.Security.Principal.CISFIdentity)HttpContext.Current.User.Identity).FullName;
        }
    }

    public static class WebletControlFactory2
    {

        public static UserControl CreateControlHost(TemplateControl host)
        {

            UserControl currentControl = host.LoadControl("UserControls\\WidgetHost.ascx") as UserControl;
            return currentControl;

        }

        public static UserControl CreateWeblet(TemplateControl host, int nControlId, string relativePath)
        {

            string strPath = "..\\" + relativePath; ;
            UserControl currentControl;
            try
            {
                currentControl = host.LoadControl(strPath) as UserControl;
                currentControl.ID = "Widget" + nControlId.ToString();
                return currentControl;

            }
            catch
            {

            }

            strPath = "\\" + relativePath;

            try
            {
                currentControl = host.LoadControl(strPath) as UserControl;
                currentControl.ID = "Widget" + nControlId.ToString();
                return currentControl;

            }
            catch
            {

            }

            return null;


        }


    }
}
